/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer;

//import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.json.JSONObject;

import android.content.res.AssetManager;
import android.os.Bundle;

import pcgen.android.JSONUtils;
import pcgen.android.Logger;
import pcgen.android.app.ActivityEx;
import pcgen.android.app.OptionMenusProvider;

//import pcgen.CharacterViewer.Message.Message;

public abstract class AppActivity extends ActivityEx 
{
	public CharacterViewerApplication getApplicationApp()
    {
    	return (CharacterViewerApplication)getApplication();
    }

    @Override
    protected void initialize(Bundle savedInstanceState)
    {
    	super.initialize(savedInstanceState);
	    	
	    initializeGameTypes();
    }
	
    protected OptionMenusProvider initializeOptionMenusProvider()
    {
    	return new AppOptionMenusProvider();
    }
    
    protected void initializeGameTypes() 
	{
    	try
    	{
    		if (getApplicationApp().getGameTypes() != null)
    			return;
    		
	    	String fileName = "gameTypes.ini";
	    	AssetManager assets = getAssets();
	        if (!existsExternalFileApplication(fileName))
	        {
	        	JSONObject json = JSONUtils.fromStream(assets.open(fileName));
	            JSONUtils.toStream(json, openExternalFileOutputApplication(fileName));
	        }
	        
	        JSONObject json = JSONUtils.fromStream(openExternalFileInputApplication(fileName));
	        getApplicationApp().setGameTypes(new GameTypes(json));
    	}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "initializeGameTypes", tr);
    	}
	}
    
//	protected void sendMessage(Message message, String broadcastIntentId)
//    {
//    	try
//    	{
//    		if (StringUtils.isEmpty(broadcastIntentId))
//    			return;
//
//			Intent broadcastIntent = new Intent(broadcastIntentId);
//    		broadcastIntent.putExtra("message", message);
//    		sendBroadcast(broadcastIntent);
//		
//    		Log.i(getClass().getSimpleName(), "Broadcast: " + message);
//
//    	}
//		catch (Throwable tr)
//		{
//			Logger.e(TAG, "sendMessage", tr);
//		}
//    }
	
	@SuppressWarnings("unused")
	private static final String TAG = AppActivity.class.getSimpleName();
}
